package vertx;

import com.hazelcast.config.Config;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.EventBusOptions;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.spi.cluster.hazelcast.HazelcastClusterManager;
import netty.util.StartAndStop;
import test.PerformanceAnalyze;

public class BusBridgeServer extends StartAndStop {

    PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    public void start() {
        Config config = new Config();
        config.getNetworkConfig().setPublicAddress("127.0.0.1").setPort(5701);
        config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true)
                .addMember("127.0.0.1:5702");
        ClusterManager mgr = new HazelcastClusterManager(config);

        VertxOptions options = new VertxOptions().setClusterManager(mgr);
        options.getEventBusOptions().setHost("127.0.0.1");

        Vertx.clusteredVertx(options, res -> {
            if (res.succeeded()) {
                Vertx vertx = res.result();
                EventBusOptions eventBusOptions = new EventBusOptions();
                EventBus eb = vertx.eventBus();

                eb.consumer("news.uk.sport", message -> {
                    performanceAnalyze.delay(System.nanoTime()- (long)message.body());
                    performanceAnalyze.meter();
                });

            } else {
                // failed!
            }
        });

    }

    public static void main(String[] args) {
        new BusBridgeServer().start();
    }
}
